package com.atguigu.gmall.seckill.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.seckill.biz.SeckillBizService;
import com.atguigu.gmall.seckill.vo.SeckillOrderSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/12/27 19:12
 * @Version 1.0
 */
@RequestMapping("/api/activity/seckill/auth")
@RestController
public class SeckillApiController {

    @Autowired
    SeckillBizService seckillBizService;

    @GetMapping("/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillCode(@PathVariable("skuId") Long skuId){


        String  code = seckillBizService.generateSeckillCode(skuId);

        return Result.ok(code);
    }

    @PostMapping("/seckillOrder/{skuId}")
    public Result seckillOrder(@PathVariable("skuId") Long skuId,
                               @RequestParam("skuIdStr")String code){


        //秒杀下单
        seckillBizService.seckillOrder(skuId,code);

        return Result.ok();
    }

    @GetMapping("/checkOrder/{skuId}")
    public Result checkOrder(@PathVariable("skuId") Long skuId){

        //检查秒杀单状态
        ResultCodeEnum codeEnum = seckillBizService.checkOrder(skuId);

        return Result.build("",codeEnum);
    }

    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody SeckillOrderSubmitVo submitVo){

        Long orderId = seckillBizService.submitOrder(submitVo);
        return Result.ok(orderId.toString());
    }

}
